package cn.edu.wzut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;


@Controller
public class LikeController{

    @Autowired
    private RedisTemplate<String, Integer> redisTemplate;

    @GetMapping("/")
    public String getHomePage(Model model) {
        ValueOperations<String, Integer> ops = redisTemplate.opsForValue();
        Integer count = ops.get("likeCount");
        if (count == null) {
            count = 0;
            ops.set("likeCount", count);
        }
        model.addAttribute("count", count);
        return "index";
    }

    @PostMapping("/like")
    public String like() {
        ValueOperations<String, Integer> ops = redisTemplate.opsForValue();
        Integer count = ops.get("likeCount");
        if (count != null) {
            count++;
            ops.set("likeCount", count);
        }
        return "redirect:/";
    }
}
